package com.zr.bookcorner.presenter;

import android.text.TextUtils;
import android.widget.LinearLayout;

import com.zh.baselibrary.netutils.BaseResp;
import com.zh.baselibrary.rxjava.RxSubscribe;
import com.zh.baselibrary.utils.ComparatorUtils;
import com.zh.baselibrary.utils.LogUtils;
import com.zh.baselibrary.utils.PhoneUtil;
import com.zh.baselibrary.utils.TTSUtils;
import com.zh.baselibrary.views.ToastView;
import com.zh.model.manager.BookInfo;
import com.zh.model.request.SubmitDetailInfo;
import com.zh.model.request.SubmitInfo;
import com.zr.bookcorner.R;
import com.zr.bookcorner.constant.Constant;
import com.zr.bookcorner.service.VoiceEvent;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.schedulers.Schedulers;

//继承上下架逻辑
public class BorrowReturnDetailPresenter extends ManagerDetailPresenter {
    private List<BookInfo> bookBorrowedList = new ArrayList<>();  //已借
    private List<BookInfo> bookBorrowNowList = new ArrayList<>();  //本次借
    private StringBuilder borrowName = new StringBuilder();;
    private StringBuilder upName= new StringBuilder();
    private StringBuilder returnName= new StringBuilder();
    private List<BookInfo> notRelationList = new ArrayList<>();
    private StringBuilder errorDetail= new StringBuilder();
    private int upReturnSize = 0;

    public void getBorrowRecord() {
        model.borrowRecord(schoolId).subscribeOn(Schedulers.io()).subscribe(new RxSubscribe<BaseResp<List<BookInfo>>>() {
            @Override
            public void onSuccess(BaseResp<List<BookInfo>> baseResp) throws Exception {
                for (BookInfo datum : baseResp.getData()) {
                    if(datum!=null) {
                        datum.setBorrow(true);
                        bookBorrowedList.add(datum);
                    }
                }
            }

            @Override
            public void onHint(String hint) {
                ToastView.showShort(hint);
            }
        });
    }

    @Override
    protected void solveStatusFormNet(List<BookInfo> data) {
        bookBorrowNowList.clear();
        bookInList.clear();
        upReturnSize = 0;
        notRelationList.clear();
        borrowName = new StringBuilder();
        upName = new StringBuilder();
        returnName = new StringBuilder();
        errorDetail = new StringBuilder();
        boolean speak = false;
        for (BookInfo info : data) {
            if(inThisList.contains(info.getRadioCode())){
                if (info.getStatus() == Constant.NOT_RELATION) {
                    speak = true;
                    notRelationList.add(info);
                }
            }
            if (outList.contains(info.getRadioCode())) {
                //借书
                if(info.getStatus()!=7&&info.getStatus()!=8) {
                    info.setStatus(Constant.BORROW);
                    bookBorrowNowList.add(info);
                    borrowName.append(info.getBookName());
                    borrowName.append("、");
                }

            }else if (inList.contains(info.getRadioCode())){

                if (info.getStatus() == Constant.UP) { //增量 无借阅还
                    LogUtils.e("无借阅");
                    info.setStatus(Constant.RETURN);
                    returnName.append(info.getBookName());
                    returnName.append("、");
                    upReturnSize++;
                    bookInList.add(info);
                }else {
                    sort(info);
                }

            }else if (info.getStatus()!=Constant.UP){ //全量 不为1 分类
                sort(info);
            }else {
                bookInList.add(info);
            }
        }
        if(speak) {
            //TTSUtils.getInstance().speak(getContext().getString(R.string.tag_not_relation));
            EventBus.getDefault().post(new VoiceEvent(getContext().getString(R.string.tag_not_relation)));
        }
        sortCabinet(bookInList);
        getView().getDataSuccess(data);
        if(isDoorClosed&&(count!=1))
            getView().doorClose();
        else
            bookGridLater();

    }
   private void sort(BookInfo info){
       int status = info.getStatus();
       if (status == Constant.NOT_UP) {
           upName.append(info.getBookName());
           upName.append("、");
           upReturnSize++;
           bookInList.add(info);
       }else if (status == Constant.RETURN || status == Constant.RETURN6 ) {
           returnName.append(info.getBookName());
           returnName.append("、");
           upReturnSize++;
           bookInList.add(info);
       }else if (info.getStatus() == Constant.NOT_RELATION) {
           upName.append(info.getBookName());
           upName.append("、");
           upReturnSize++;
           bookInList.add(info);
       }
   }
    @Override
    public void toSubmit() {
        SubmitInfo submitInfo = new SubmitInfo();
        String uuid = PhoneUtil.getUUID(getContext());
        List<SubmitDetailInfo> list = new ArrayList<>();
        for (BookInfo bookInfo : bookInList) {   //第一栏中状态不为1的，0的上架 还有5 6
            int status = bookInfo.getStatus();
            if (status == Constant.UP || status == Constant.NOT_IMPORT || status == Constant.NOT_SCHOOL)
                continue;

            if (status == Constant.NOT_UP||status == Constant.NOT_RELATION) {
                status = Constant.UP;
            }

            SubmitDetailInfo info = new SubmitDetailInfo();
            info.setRadioCode(bookInfo.getRadioCode());
            info.setStatus(status);
            list.add(info);
        }
        for (BookInfo bookInfo : bookBorrowNowList) {
            SubmitDetailInfo info = new SubmitDetailInfo();
            info.setRadioCode(bookInfo.getRadioCode());
            info.setStatus(bookInfo.getStatus());
            list.add(info);
        }
        submitInfo.setCabinetNum(boxNum);
        submitInfo.setEquipmentCode(uuid);
        submitInfo.setBookList(list);
        submitOperate(submitInfo);
    }

    public List<BookInfo> getBookBorrowNowList() {
        return bookBorrowNowList;
    }

    public List<BookInfo> getBookBorrowAllList() {
        List<BookInfo> all = new ArrayList<>();
        all.addAll(bookBorrowNowList);
        all.addAll(bookBorrowedList);
        return all;
    }

    public List<BookInfo> getInList() {
        return bookInList;
    }

    public int getUpReturnSize() {
        return upReturnSize;
    }

    public String getName(int status) {
        String sb = "";
        if (status == Constant.BORROW)
            sb = borrowName.toString();
        else if (status == Constant.UP)
            sb = upName.toString();
        else if (status == Constant.RETURN)
            sb = returnName.toString();

        if (!TextUtils.isEmpty(sb)) {
            sb = sb.substring(0, sb.length() - 1);
        }
        return sb;
    }
    public String getError(){
        if(notRelationList.size()!=0){
            errorDetail.append("标签号为");
            StringBuilder tagList = new StringBuilder();
            for(int i=0;i<notRelationList.size();i++){
                tagList.append(notRelationList.get(i).getRadioCode());
                if(i!=notRelationList.size()-1)
                    tagList.append("、");
            }
            errorDetail.append(changeColor(tagList.toString())).append("异常，共").append(changeColor(notRelationList.size()+"")).append("本，");
            errorDetail.append("异常原因：二维码与书名未关联");
        }
        return  errorDetail.toString();
    }
    public int getErrorSize(){
        return  notRelationList.size();
    }

}
